package com.szqhyxj.youxiangju.biz.realNameAuth.bankcard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.PhotoBridge;
import com.szqhyxj.youxiangju.biz.realNameAuth.success.NameAuthenticationSuccessActivity;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.picker.OptionPicker;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.PickerUtil;
import com.szqhyxj.youxiangju.sys.components.view.popview.SingleDialog;
import com.szqhyxj.youxiangju.sys.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CustomAdapt;

/**
 * Created by leung on 2018/10/30.
 */

public class BankCardAuthActivity extends BaseActivity implements BankCardAuthContract.View, NavigationBar.NavigationBarListener ,CustomAdapt{
    private static final int REQUEST_TAKE_PHOTO_CARD = 5;
    private static final int REQUEST_GALLERY_CARD = 6;
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.msm)
    EditText msm;
    @BindView(R.id.backOnclick)
    Button backOnclick;
    @BindView(R.id.commit)
    Button commit;
    private Boolean cardFlag = false;
    private static String username;
    @BindView(R.id.debitard)
    ImageView debitard;
    @BindView(R.id.tipcard)
    TextView tipcard;
    @BindView(R.id.userName_card)
    TextView userNameCard;
    @BindView(R.id.cardno)
    EditText cardno;
    @BindView(R.id.issuer)
    TextView issuer;
    @BindView(R.id.issuerselect)
    ImageView issuerselect;
    @BindView(R.id.name_authentication_phone)
    EditText nameAuthenticationPhone;
    @BindView(R.id.getMessage)
    TextView getMessage;

    public static void start(Activity activity, String name) {
        Intent intent = new Intent();
        intent.setClass(activity, BankCardAuthActivity.class);
        activity.startActivity(intent);
        username = name;
    }

    private BankCardAuthContract.Presenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_authentication_card);
        ButterKnife.bind(this);
        presenter = new BankCardPresenter(this);
        nameAuthenticationPhone.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setPhone(s.toString());
            }
        });
        userNameCard.setText(username);
        presenter.setusername(username);
        msm.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setverCode(s+"");
            }
        });
        cardno.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setbankCardNo(s+"");
            }
        });
        navigationBar.setNavigationBarListener(this);
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }

    @Override
    public boolean isBaseOnWidth(){
        return true;
    }

    @Override
    public float getSizeInDp(){
        return 360;
    }

    public abstract static class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }


    @OnClick({R.id.shootingDebitCard, R.id.getIssuerArea, R.id.getMessage, R.id.backOnclick, R.id.commit})
    public void onClick(View view) {
        final SingleDialog singleDialog = new SingleDialog(this);
        switch (view.getId()) {
            case R.id.shootingDebitCard:
                singleDialog.setISharePlatformListener(new SingleDialog.ISharePlatformListener() {
                    @Override
                    public void sharePlatform(String platform) {

                    }

                    @Override
                    public void onClickAlbum(View view) {

                        presenter.selectFromGalleryForCard();
                        singleDialog.dismiss();
                    }

                    @Override
                    public void onTakePhoto(View view) {
                        try {
                            presenter.takePhotoForCard(BankCardAuthActivity.this);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        singleDialog.dismiss();
                    }
                });
                singleDialog.show();

                break;
            case R.id.getIssuerArea:
                presenter.getIssuer();
                break;
            case R.id.getMessage:
                presenter.acquireVerificationCode();
                break;
            case R.id.backOnclick:
                finish();
                break;
            case R.id.commit:
                presenter.setverCode(msm.getText().toString());
                presenter.uploadBankCard();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_CANCELED) {
            return;
        }
        if (requestCode == REQUEST_TAKE_PHOTO_CARD) {
            try {
                presenter.idcardCardFromTakePhoto(this);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        if (requestCode == REQUEST_GALLERY_CARD) {
            File tempFile = FileUtils.uri2File(this, data.getData());
            presenter.idcardCardFromGallery(tempFile);
        }
    }

    @Override
    public void navToTakePhotoCard(File photoFile) {
        PhotoBridge.startToTakePicture(this, REQUEST_TAKE_PHOTO_CARD, photoFile);
    }

    @Override
    public void navToGalleryForCard() {
        PhotoBridge.startToGallery(this, REQUEST_GALLERY_CARD);
    }

    @Override
    public void showOcrFail(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showBankCardUploadSuccess(String fileUrl) {
        Toast.makeText(this, fileUrl, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showBankCardUploadFail(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showCardno(String s) {

        cardno.setText(s);
        presenter.setbankCardNo(s);


    }

    private String issuingBank;  //发卡行
    private String btCode;  //发卡行code

    @Override
    public void showIssuser(final List<BankList> lists) {

        String[] bs = new String[lists.size()];
        for (int i = 0; i < lists.size(); i++) {
            bs[i] = lists.get(i).getBtName();
        }
        PickerUtil.onOptionPicker(this, bs, new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                issuer.setText(item);
                issuingBank = item;
                btCode = lists.get(index).getBtCode();
                presenter.setbtName(issuingBank);
                presenter.setbtCode(btCode);
                issuerselect.setVisibility(View.GONE);
                Log.e("------------", "选择了" + issuingBank + "--" + btCode);
            }
        });
    }

    @Override
    public void showAcquiringVerificationCode(int number) {
        getMessage.setText(String.format("%ss", String.valueOf(number)));
        getMessage.setEnabled(false);
    }

    @Override
    public void showAcquiredVerificationCode() {
        getMessage.setText("发送");
        getMessage.setEnabled(true);
    }

    @Override
    public void showAcquiredVerificationCodeSuccess() {
        Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showAcquiredVerificationCodeFail() {
        Toast.makeText(this, "请求验证码失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void uploadSuccessCard() {
        cardFlag = true;
    }

    @Override
    public void commitSuccess() {
        tipcard.setVisibility(View.GONE);
        NameAuthenticationSuccessActivity.start(this);
    }

    @Override
    public void commitFial(String s) {
       /* tipcard.setVisibility(View.VISIBLE);
        tipcard.setText("请核对信息在上传！");*/
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showCommit() {
        commit.setBackground(getResources().getDrawable(R.drawable.background_btn04));
        commit.setEnabled(true);
    }

    @Override
    public void showEnCommit() {
        commit.setBackground(getResources().getDrawable(R.drawable.background_btn_grey04));
        commit.setEnabled(false);
    }

    @Override
    public void previewBank(File file) {
        LoadImageUtils.loadImage(debitard,file);
    }

    @Override
    public void showBank() {
        debitard.setImageDrawable(getResources().getDrawable(R.mipmap.yinhangka));
    }

    @Override
    public void showbankCard(CardInfo cardInfo) {

    }

    @Override
    public void showUsername(String s) {


    }


    @Override
    public void showOCRTip(String s) {
        tipcard.setVisibility(View.VISIBLE);
        tipcard.setText(s);
    }

    @Override
    public void enshowOCRTip() {
        tipcard.setVisibility(View.GONE);
    }


}
